/*
 * Copyright (c) 2023, LXMS (lxinet.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.lxinet.lxms.system.service;

import cn.lxinet.lxms.framework.core.helper.PageHelper;
import cn.lxinet.lxms.framework.core.utils.Assert;
import cn.lxinet.lxms.framework.mybatis.service.SimpleService;
import cn.lxinet.lxms.system.entity.SysRole;
import cn.lxinet.lxms.system.mapper.SysRoleMapper;
import cn.lxinet.lxms.system.message.SystemErrorCode;
import cn.lxinet.lxms.system.vo.SysRoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色
 *
 * @author zcx
 * @date 2023/07/08
 */
@Slf4j
@Service
public class SysRoleService extends SimpleService<SysRole> {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    public List<SysRoleVo> list(String name, String code, Integer status){
        return sysRoleMapper.list(PageHelper.getPage(), name, code, status);
    }

    /**
     * 选择框列表
     *
     * @return {@link List}<{@link SysRoleVo}>
     */
    public List<SysRoleVo> simpleList(){
        return sysRoleMapper.simpleList();
    }

    public void add(SysRole sysRole){
        Assert.isNull(sysRoleMapper.findByCode(sysRole.getCode()), SystemErrorCode.CODE_EXIST);
        super.insert(sysRole);
    }

    public void update(SysRole sysRole){
        SysRole codeRole = sysRoleMapper.findByCode(sysRole.getCode());
        Assert.isTrue(codeRole == null || codeRole.getId().equals(sysRole.getId()), SystemErrorCode.CODE_EXIST);
        super.updateById(sysRole);
    }

    public void delete(Long id){
        SysRole sysRole = findById(id);
        Assert.notNull(sysRole, SystemErrorCode.ROLE_NOT_EXIST);
        Assert.isTrue(!sysRole.isAdministrator(), SystemErrorCode.ROLE_ADMINISTRATOR_CAN_NOT_DEL);
        super.deleteById(id);
        //todo 跟角色关联的数据要删除，如人员
    }

}